﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Public
{
	public partial class ViewDrawingForm : System.Windows.Forms.Form
	{
		public ViewDrawingForm(List<Element> viewList, List<Element> sheetList, List<Element> tbList, Document doc, string type = "0")
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.viewList = viewList;
			this.sheetList = sheetList;
			this.tbList = tbList;
			this.doc = doc;
			this.Text = "布图管理";
			if (type == "1")
			{
				this.InitFormByType();
				this.Text = "图纸管理";
				return;
			}
			if (viewList.Count == 0)
			{
				YJKMessageBox.Information("没有需要出图的视图！");
			}
		}

		private void InitFormByType()
		{
			base.Size = new Size(237, 484);
            this.groupBox2.Location = new System.Drawing.Point(7, 10);
            this.btnOK.Location = new System.Drawing.Point(28, 411);
            this.btnCancel.Location = new System.Drawing.Point(120, 411);
			this.groupBox1.Visible = false;
			this.btnLeft.Visible = false;
			this.btnRight.Visible = false;
		}

		protected void initControls()
		{
			foreach (Element element in this.viewList)
			{
                if (element != null && element is Autodesk.Revit.DB.View)
				{
                    Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
					this.InittvViews(view);
				}
			}
			this.SelectFirstItem();
			foreach (Element element2 in this.sheetList)
			{
				if (element2 != null && element2 is ViewSheet)
				{
					ViewSheet viewSheet = element2 as ViewSheet;
					TreeNode treeNode = this.tvDrawing.Nodes.Add(viewSheet.SheetNumber + " - " + viewSheet.Name);
					treeNode.Tag = viewSheet;
					foreach (ElementId elementId in viewSheet.GetAllViewports())
					{
						treeNode.Nodes.Add(this.doc.GetElementById(elementId).GetParameter(BuiltInParameter.VIEW_NAME).AsString()).Tag = elementId;
					}
				}
			}
			if (this.tvDrawing.Nodes != null && this.tvDrawing.Nodes.Count > 0)
			{
				this.tvDrawing.SelectedNode = this.tvDrawing.Nodes[0];
			}
			this.tvDrawing.ExpandAll();
			this.tvViews.ExpandAll();
		}

        private void InittvViews(Autodesk.Revit.DB.View view)
		{
			if (view == null)
			{
				return;
			}
			ListItem tag = new ListItem(view.Name, view.Id.IntegerValue.ToString(), view);
			TreeNode treeNode = null;
			string viewTypeName = this.GetViewTypeName(view.ViewType);
			foreach (object obj in this.tvViews.Nodes)
			{
				TreeNode treeNode2 = (TreeNode)obj;
				if (treeNode2 != null && treeNode2.Name == viewTypeName)
				{
					treeNode = treeNode2;
					break;
				}
			}
			TreeNode treeNode3;
			if (treeNode != null)
			{
				treeNode3 = treeNode;
			}
			else
			{
				treeNode3 = this.tvViews.Nodes.Add(this.GetViewTypeName(view.ViewType), viewTypeName);
			}
			TreeNode treeNode4 = new TreeNode(view.Name);
			treeNode4.Name = view.Name;
			treeNode4.Tag = tag;
			treeNode3.Nodes.Add(treeNode4);
		}

		private string GetViewTypeName(ViewType viewType)
		{
			switch ((int)viewType)
			{
			case 0:
				return "未定义";
			case 1:
				return "楼层平面";
			case 2:
				return "天花板平面";
			case 3:
				return "立面";
			case 4:
				return "三维";
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
				break;
			case 10:
				return "详图平面";
			default:
                if (viewType == (Autodesk.Revit.DB.ViewType)117)
				{
					return "剖面";
				}
				break;
			}
			return "楼层平面";
		}

		private void SelectFirstItem()
		{
			foreach (object obj in this.tvViews.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode != null && treeNode.Nodes.Count > 0)
				{
					this.tvViews.SelectedNode = TreeViewTools.GetFirstNode(treeNode);
					this.liSelectItem.Clear();
					this.liSelectItem.Add(this.tvViews.SelectedNode.Tag as ListItem);
					this.viewsSelectNode = this.tvViews.SelectedNode;
					break;
				}
			}
			this.tvViews.Select();
		}

		private void ViewDrawingForm_Load(object sender, EventArgs e)
		{
			this.initControls();
		}

		private void btnRight_Click(object sender, EventArgs e)
		{
			if (this.treeSelectNode != null && this.liSelectItem.Count > 0)
			{
				this.isSelectChange = false;
				TreeNode treeNode;
				if (this.tvDrawing.SelectedNode.Tag is ViewSheet)
				{
					treeNode = this.tvDrawing.SelectedNode;
				}
				else
				{
					treeNode = this.tvDrawing.SelectedNode.Parent;
				}
				foreach (ListItem listItem in this.liSelectItem)
				{
					treeNode.Nodes.Add(listItem.Name).Tag = listItem.Tag;
					TreeNode[] array = this.tvViews.Nodes.Find(listItem.Name, true);
					if (array != null && array.Count<TreeNode>() > 0)
					{
						this.tvViews.Nodes.Remove(array[0]);
					}
				}
				this.liSelectItem.Clear();
				this.isSelectChange = true;
				this.SelectFirstItem();
				this.tvDrawing.ExpandAll();
				this.IsChangge = true;
				return;
			}
			if (this.tvDrawing.Nodes == null || this.tvDrawing.Nodes.Count < 1)
			{
				YJKMessageBox.Information("请先创建图纸");
			}
		}

		private void tvViews_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void tvDrawing_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (e.Node != null)
			{
				if (this.treeSelectNode != null)
				{
                    this.treeSelectNode.BackColor = System.Drawing.Color.Transparent;
                    this.treeSelectNode.ForeColor = System.Drawing.Color.Black;
				}
				this.treeSelectNode = e.Node;
                this.treeSelectNode.BackColor = System.Drawing.Color.DodgerBlue;
                this.treeSelectNode.ForeColor = System.Drawing.Color.White;
				if (e.Node.Tag is ViewSheet)
				{
					this.btnEditSheet.Enabled = true;
					this.btnDelSheet.Enabled = true;
					this.btnGoSheet.Enabled = true;
					this.btnLeft.Enabled = false;
					return;
				}
				this.btnDelSheet.Enabled = false;
				this.btnEditSheet.Enabled = false;
				this.btnGoSheet.Enabled = false;
				this.btnLeft.Enabled = true;
			}
		}

		private void btnLeft_Click(object sender, EventArgs e)
		{
			if (this.tvDrawing.SelectedNode != null)
			{
				if (this.tvDrawing.SelectedNode.Tag is ViewSheet)
				{
					return;
				}
				ListItem listItem = new ListItem();
                Autodesk.Revit.DB.View view;
				if (this.tvDrawing.SelectedNode.Tag is ElementId)
				{
                    view = (this.doc.GetElementById((this.doc.GetElementById(this.tvDrawing.SelectedNode.Tag as ElementId) as Viewport).ViewId) as Autodesk.Revit.DB.View);
				}
				else
				{
                    view = (this.tvDrawing.SelectedNode.Tag as Autodesk.Revit.DB.View);
				}
				listItem.Name = view.Name;
				listItem.Value = view.Id.IntegerValue.ToString();
				listItem.Tag = view;
                this.InittvViews(listItem.Tag as Autodesk.Revit.DB.View);
				this.treeSelectNode = null;
				this.tvDrawing.SelectedNode.Remove();
				this.IsChangge = true;
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			TreeNodeCollection nodes = this.tvDrawing.Nodes;
			List<ElementId> list = new List<ElementId>();
			foreach (object obj in nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Tag is ViewSheet)
				{
					IEnumerable<ElementId> allViewports = (treeNode.Tag as ViewSheet).GetAllViewports();
					TreeNodeCollection nodes2 = treeNode.Nodes;
					bool flag = false;
					foreach (ElementId elementId in allViewports)
					{
						foreach (object obj2 in nodes2)
						{
							TreeNode treeNode2 = (TreeNode)obj2;
							if (treeNode2.Tag is ElementId && (treeNode2.Tag as ElementId).IntegerValue == elementId.IntegerValue)
							{
								flag = true;
								break;
							}
						}
						if (!flag)
						{
							list.Add(elementId);
						}
						flag = false;
					}
				}
			}
			if (list.Count > 0)
			{
				foreach (ElementId elementId2 in list)
				{
					Viewport viewport = this.doc.GetElementById(elementId2) as Viewport;
					(this.doc.GetElementById(viewport.SheetId) as ViewSheet).DeleteViewport(viewport);
				}
			}
			foreach (object obj3 in nodes)
			{
				TreeNode treeNode3 = (TreeNode)obj3;
				if (treeNode3.Tag is ViewSheet)
				{
					ViewSheet viewSheet = treeNode3.Tag as ViewSheet;
					foreach (object obj4 in treeNode3.Nodes)
					{
						TreeNode treeNode4 = (TreeNode)obj4;
                        if (treeNode4.Tag is Autodesk.Revit.DB.View)
						{
                            Autodesk.Revit.DB.View view = treeNode4.Tag as Autodesk.Revit.DB.View;
							Viewport.Create(this.doc, viewSheet.Id, view.Id, new XYZ(1.0, 1.0, 0.0));
						}
					}
				}
			}
			if (this.IsChangge)
			{
				YJKMessageBox.Information("操作成功！");
			}
			base.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void btnDelSheet_Click(object sender, EventArgs e)
		{
			if (this.tvDrawing.Nodes != null && this.tvDrawing.SelectedNode != null)
			{
				TreeNode treeNode = this.tvDrawing.SelectedNode;
				if (!(treeNode.Tag is ViewSheet))
				{
					treeNode = treeNode.Parent;
				}
				ViewSheet viewSheet = treeNode.Tag as ViewSheet;
				if (YJKMessageBox.Warning("确定要删除 " + viewSheet.Name + " 图纸吗？") == DialogResult.OK)
				{
					if (this.doc.ActiveView.Id.Equals(viewSheet.Id))
					{
						YJKMessageBox.Information("该图纸是当前激活视图，不能被删除！");
						return;
					}
					foreach (object obj in treeNode.Nodes)
					{
						TreeNode treeNode2 = (TreeNode)obj;
						ListItem listItem = new ListItem();
                        Autodesk.Revit.DB.View view;
						if (treeNode2.Tag is ElementId)
						{
                            view = (this.doc.GetElementById((this.doc.GetElementById(treeNode2.Tag as ElementId) as Viewport).ViewId) as Autodesk.Revit.DB.View);
						}
						else
						{
                            view = (treeNode2.Tag as Autodesk.Revit.DB.View);
						}
						listItem.Name = view.Name;
						listItem.Value = view.Id.IntegerValue.ToString();
						listItem.Tag = view;
                        this.InittvViews(listItem.Tag as Autodesk.Revit.DB.View);
						this.treeSelectNode = null;
					}
					this.tvDrawing.Nodes.Remove(treeNode);
					this.doc.DeleteElement(viewSheet);
				}
			}
		}

		private void btnEditSheet_Click(object sender, EventArgs e)
		{
			if (this.tvDrawing.SelectedNode != null && this.tvDrawing.SelectedNode.Tag is ViewSheet)
			{
				ViewSheet viewSheet = this.tvDrawing.SelectedNode.Tag as ViewSheet;
				AddTitleBlocksForm addTitleBlocksForm = new AddTitleBlocksForm(this.doc, viewSheet);
				DialogResult dialogResult = addTitleBlocksForm.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					if (addTitleBlocksForm.fs != null)
					{
						if (addTitleBlocksForm.titleBlock != null)
						{
							(addTitleBlocksForm.titleBlock as FamilyInstance).Symbol = addTitleBlocksForm.fs;
						}
						else
						{
							UnifiedModified.ActivateFamilySymbol(addTitleBlocksForm.fs);
							this.doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), addTitleBlocksForm.fs, viewSheet);
						}
					}
					Parameter parameter = viewSheet.GetParameter(BuiltInParameter.SHEET_NAME);
					if (parameter != null && !parameter.AsString().Equals(addTitleBlocksForm.sheetName))
					{
						parameter.Set(addTitleBlocksForm.sheetName);
					}
					Parameter parameter2 = viewSheet.GetParameter(BuiltInParameter.SHEET_NUMBER);
					if (parameter2 != null)
					{
						parameter2.Set(addTitleBlocksForm.sheetNum);
					}
					this.tvDrawing.SelectedNode.Text = addTitleBlocksForm.sheetNum + " - " + addTitleBlocksForm.sheetName;
					return;
				}
			}
			else
			{
				YJKMessageBox.Information("请选择所要修改的图纸");
			}
		}

		private void btnNewSheet_Click(object sender, EventArgs e)
		{
			AddTitleBlocksForm addTitleBlocksForm = new AddTitleBlocksForm(this.doc, null);
			DialogResult dialogResult = addTitleBlocksForm.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				ViewSheet viewSheet = ViewSheet.Create(this.doc, this.doc.GetElementById(addTitleBlocksForm.fs.Id).Id);
				if (viewSheet != null)
				{
					Parameter parameter = viewSheet.GetParameter(BuiltInParameter.SHEET_NUMBER);
					if (parameter != null)
					{
						parameter.Set(addTitleBlocksForm.sheetNum);
					}
					Parameter parameter2 = viewSheet.GetParameter(BuiltInParameter.SHEET_NAME);
					if (parameter2 != null)
					{
						parameter2.Set(addTitleBlocksForm.sheetName);
					}
					TreeNode treeNode = new TreeNode(addTitleBlocksForm.sheetNum + " - " + addTitleBlocksForm.sheetName);
					treeNode.Tag = viewSheet;
					this.tvDrawing.Nodes.Add(treeNode);
					this.tvDrawing.SelectedNode = treeNode;
					return;
				}
				YJKMessageBox.Information("创建图纸失败");
			}
		}

		private void btnGoSheet_Click(object sender, EventArgs e)
		{
			if (this.tvDrawing.SelectedNode != null && this.tvDrawing.SelectedNode.Tag is ViewSheet)
			{
				ViewSheet activeView = this.tvDrawing.SelectedNode.Tag as ViewSheet;
				this.uiDoc.ActiveView = activeView;
			}
		}

		private void tvViews_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (e.Node != null)
			{
				if (this.viewsSelectNode != null)
				{
                    this.viewsSelectNode.BackColor = System.Drawing.Color.Transparent;
                    this.viewsSelectNode.ForeColor = System.Drawing.Color.Black;
				}
				this.viewsSelectNode = e.Node;
                this.viewsSelectNode.BackColor = System.Drawing.Color.DodgerBlue;
                this.viewsSelectNode.ForeColor = System.Drawing.Color.White;
			}
			if (this.tvViews.SelectedNode.Tag is ListItem)
			{
				this.btnRight.Enabled = true;
			}
			else
			{
				this.btnRight.Enabled = false;
			}
			if (this.isSelectChange && this.tvViews.SelectedNode.Tag is ListItem)
			{
				this.liSelectItem.Clear();
				this.liSelectItem.Add(this.tvViews.SelectedNode.Tag as ListItem);
			}
		}

		private UIDocument uiDoc;

		private List<Element> viewList;

		private List<Element> sheetList;

		private List<Element> tbList;

		private Document doc;

		private TreeNode treeSelectNode;

		private TreeNode viewsSelectNode;

		private List<ListItem> liSelectItem = new List<ListItem>();

		private bool IsChangge;

		private bool isSelectChange = true;
	}
}
